# -*- coding = utf-8 -*-
# @Time :2024/2/1 20:13
# @Author :invictusQAQ
# @Description :
# @File :  files_page.py

from time import sleep
from selenium.webdriver.common.by import By
from config.config import files_url
from base.base_page import BasePage
import win32gui
import win32con


class FilesPage(BasePage):
    upload = (By.ID, 'uploadButton')
    click_upload = (By.XPATH, '//*[@id="upload-content"]/div/div[2]/a[2]/span')
    new_folder = (By.XPATH, '//*[@id="app"]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/button/span')
    filename_wrapper = (By.CSS_SELECTOR, '.el-input__wrapper')
    filename_input = (By.CSS_SELECTOR, '.el-input__inner')
    click_new_folder = (By.XPATH, '/html/body/div[5]/div/div/footer/span/button[2]')
    select_all = (By.XPATH, '//*[@id="app"]/div/div[3]/div/div/div[3]/div/div[1]/div[2]/table/thead/tr/th['
                            '1]/div/label/span/span')
    click_download = (By.XPATH, '//*[@id="app"]/div/div[3]/div/div/div[1]/div[1]/div/div[3]/button')

    def __open_dialog_window_and_upload(self, file_path):
        win1 = win32gui.FindWindow('#32770', '打开')
        win2 = win32gui.FindWindowEx(win1, 0, 'ComboBoxEx32', None)
        win3 = win32gui.FindWindowEx(win2, 0, 'ComboBox', None)
        edit = win32gui.FindWindowEx(win3, 0, 'Edit', None)
        button = win32gui.FindWindowEx(win1, 0, 'Button', '打开(&o)')
        win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
        win32gui.SendMessage(win1, win32con.WM_COMMAND, 1, button)

    def upload_file(self, file_path):
        self.visit(files_url)
        self.click(self.upload)
        sleep(1)
        self.click(self.click_upload)
        sleep(1)
        self.__open_dialog_window_and_upload(file_path)
        sleep(10)

    def create_folder(self, name):
        self.visit(files_url)
        self.click(self.new_folder)
        # 显式等待与隐式等待暂时无法处理，使用强制等待
        sleep(1)
        self.focus_input(name)
        self.click(self.click_new_folder)
        sleep(10)

    def download_all(self):
        self.visit(files_url)
        self.click(self.select_all)
        self.click(self.click_download)
        sleep(1000)
